package com.example.project3.util;

import org.springframework.stereotype.Component;

/**
 * @author xh169
 * @version 1.0
 * @description: 本地线程保存token
 * @date 2023/6/1 18:40
 */
@Component
public class ThreadLocalToken {

    private final ThreadLocal<String> threadLocal = new ThreadLocal<>();

    /**
     * 获取token
     *
     * @return token
     */
    public String getToken() {
        return threadLocal.get();
    }

    /**
     * 放入token
     *
     * @param token token
     */
    public void setToken(String token) {
        threadLocal.set(token);
    }

    /**
     * 清空本地线程，防止内存溢出
     */
    public void clear() {
        threadLocal.remove();
    }
}
